package com.dream.base.web.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 
 * @author Evan
 * @date 2013-8-25
 * 
 */
public class CharacterFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain ) throws IOException, ServletException {
		
		if(!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)){
			throw new ServletException("the bad http request or response!");
		}
		HttpServletRequest httpRequest = (HttpServletRequest)request;
		Map<?, ?> map = httpRequest.getParameterMap();
		for(Object obj : map.values()){
			
			if(obj instanceof String[]){
				String[] params = (String[])obj;
				for(int i =0 ; i<params.length;i++){
					params[i] = params[i].replaceAll("<", "&lt;");
					params[i] = params[i].replaceAll(">", "&gt;");
				}
			}
		}
		filterChain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}
